package org.linlinjava.litemall.admin.service;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.admin.dto.GoodsAllinone;
import org.linlinjava.litemall.admin.vo.CatVo;
import org.linlinjava.litemall.admin.vo.VendorRegVo;
import org.linlinjava.litemall.core.qcode.QCodeService;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.util.bcrypt.BCryptPasswordEncoder;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.linlinjava.litemall.admin.util.AdminResponseCode.GOODS_NAME_EXIST;

@Service
public class AdminVendorService {
    private final Log logger = LogFactory.getLog(AdminVendorService.class);

    @Autowired
    private VendorService vendorService;

    @Autowired
    private LitemallAdminService adminService;

    public List<Vendor> query(String vendorCode, String vendorName, Integer status, Integer page, Integer limit, String sort, String order) {
        if(status == -100) {
            status = null;
        }
        return vendorService.query(vendorCode, vendorName, status, page, limit, sort, order);
    }

    @Transactional
    public void add(Vendor vendor) {
        vendorService.add(vendor);
    }

    public Vendor findById(Integer id) {
        return vendorService.findById(id);
    }

    @Transactional
    public int updateById(Vendor vendor) {
        return vendorService.updateById(vendor);
    }

    @Transactional
    public void deleteById(Integer id) {
        //删除店员及登录账号
        deleteEmployeeByVendorId(id);

        vendorService.deleteById(id);
    }

    public List<VendorEmployee> getEmployees(Integer vendorId) {
        return vendorService.getEmployees(vendorId);
    }

    @Transactional
    public void deleteEmployeeById(Integer id) {
        vendorService.deleteEmployeeById(id);
    }

    @Transactional
    public void deleteEmployee(VendorEmployee employee) {
        List<LitemallAdmin> admins = adminService.findAdmin(employee.getUsername());
        if(!admins.isEmpty()) {
            adminService.deleteById(admins.get(0).getId());
            //TODO 多个怎么办？
        }

        deleteEmployeeById(employee.getId());
    }
    @Transactional
    public void deleteEmployeeByVendorId(Integer vendorId) {
        List<VendorEmployee> employees = vendorService.getEmployees(vendorId);
        for(VendorEmployee e : employees) {
            this.deleteEmployee(e);
        }
    }

    @Transactional
    public int approve(Vendor v) {
        //如果审核通过，生成店员管理员账号
        if(v.getStatus() == 0) {
            List<VendorEmployee> employees = vendorService.getEmployees(v.getId());
            for(VendorEmployee e : employees) {
                createAdmin(e);
            }
        }

        return this.updateById(v);
    }

    @Transactional
    public void addEmployee(VendorEmployee employee) {
        vendorService.addEmployee(employee);
        if(!adminService.findAdmin(employee.getUsername()).isEmpty()) {
            throw new RuntimeException(employee.getUsername() + "已被注册");
        }

        this.createAdmin(employee);
    }

    //生成管理员用户
    private void createAdmin(VendorEmployee employee) {
        LitemallAdmin admin = new LitemallAdmin();
        admin.setUsername(employee.getUsername());
        admin.setPassword(employee.getPassword());
        admin.setVendorId(employee.getVendorId());
        Integer[] roleIds = {Integer.valueOf(4)};
        admin.setRoleIds(roleIds);

        adminService.add(admin);
    }

    @Transactional
    public int updateEmployeeById(VendorEmployee employee) {
        return vendorService.updateEmployeeById(employee);
    }

    @Transactional
    public void doReg(VendorRegVo regVo) {
        Vendor vendor = regVo.toVendor();
        vendorService.add(vendor);

        VendorEmployee employee = regVo.toVendorEmployee(vendor.getId());

        vendorService.addEmployee(employee);
    }

}
